package com.aaa.controller;

import com.aaa.entity.Menu;
import com.aaa.entity.Role;
import com.aaa.service.MenuService;
import com.aaa.service.RoleService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @RequestMapping("/toList")
    public String toIndex(){
        return "role/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public TableData<Role> listData(@RequestParam(defaultValue = "") String keyword){
        TableData<Role> tableData = roleService.listAll(keyword);
        return tableData;
    }

    @RequestMapping("/toEdit")
    public String toEdit(){
        return "role/edit";
    }

    /**
     * 新增修改
     * @param role
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DefaultMsg saveOrUpdate(Role role,String mids){
        DefaultMsg defaultMsg = roleService.saveOrUpdate(role,mids);
        return defaultMsg;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public DefaultMsg delete(Integer id){
        DefaultMsg defaultMsg = roleService.delete(id);
        return defaultMsg;

    }


    /**
     * 查询所有的菜单
     * @return
     */
    @RequestMapping("/listAllMenus")
    @ResponseBody
    public List<Menu> listAllMenus(){
        List<Menu> menus = menuService.listAllByLevel();
        return  menus;
    }

    /**
     * 查询角色能操作的模块
     * @param id 角色id
     * @return
     */
    @RequestMapping("/listRoleMenus")
    @ResponseBody
    public List<Menu> listRoleMenus(Long id){
        List<Menu> menus = menuService.listRoleMenus(id);
        return  menus;
    }
}
